﻿Imports System.Data
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports TepcelDominio
Imports TepcelDAL.DBManager
Public Class MotivoTrasladoDAL : Inherits DBManager
    Public Function NuevoMotivo(ByVal motivo As MotivoTraslado) As String
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("SPInsertMotivo")

        comando.Parameters.Add("@codigoMotivo", SqlDbType.Char, 3).Value = motivo.codigoMotivo
        comando.Parameters.Add("@nombreMotivo", SqlDbType.VarChar, 100).Value = motivo.nombreMotivo

        comando.Parameters("@codigoMotivo").Direction = ParameterDirection.InputOutput

        MyBase.ExecuteQuery(comando)
        motivo.codigoMotivo = comando.Parameters("@codigoMotivo").Value

        Return motivo.codigoMotivo

    End Function

    Public Function GetAllFromMotivo() As List(Of MotivoTraslado)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("GetAllFromMotivoTraslado")
        Return SelectObjectFactory(comando)

    End Function

    Public Function SearchFromMotivo(ByVal texto As String) As List(Of MotivoTraslado)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("searchMotivo")

        comando.Parameters.Add("@texto", SqlDbType.VarChar, 200).Value = texto
        Return SelectObjectFactory(comando)

    End Function

    Public Function DeleteMotivo(ByVal codigoMotivo As String) As Boolean
        Dim correcto As Boolean = False
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("DeleteMotivo")

        comando.Parameters.Add("@codigoMotivo", SqlDbType.Char, 5).Value = codigoMotivo

        If MyBase.ExecuteQuery(comando) > 0 Then
            correcto = True
        End If
        Return correcto
    End Function

    Private Function SelectObjectFactory(ByVal comando As SqlCommand) As List(Of MotivoTraslado)
        Dim Lista As New List(Of MotivoTraslado)
        Using reader As SqlDataReader = MyBase.ExecuteReader(comando)
            While reader.Read
                Dim Motivo As New MotivoTraslado(reader.GetString(0), reader.GetString(1))
                Lista.Add(Motivo)
                Motivo = Nothing
            End While
        End Using
        Return Lista
    End Function

End Class
